package com.lph.util;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;


import com.lph.model.Menber;

public class ExcelUtil {
	public static void out(List<Menber> list,String fileName){
		try {
			//工作薄
			Workbook work = null;
			//创建1997-2003
			work = new HSSFWorkbook();//后缀名xls
			//创建2007以上版本
			//work = new XSSWorkbook();//后缀名xlsx
			
			//工作表
			Sheet sheet = work.createSheet("Sheet1");
			//创建行
			Row row = sheet.createRow(0);
			//把数据添加到单元格
			row.createCell(0).setCellValue("姓名");
			row.createCell(1).setCellValue("身份证号码");
			row.createCell(2).setCellValue("性别");
			row.createCell(3).setCellValue("出生日期");
			row.createCell(4).setCellValue("联系方式");
			row.createCell(5).setCellValue("家住地址");
			int i = 1;
			for(Menber menber : list){
				//创建行
				row = sheet.createRow(i);
				i++;
				//创建单元格
				//把数据添加到单元格
				row.createCell(0).setCellValue(menber.getName());
				row.createCell(1).setCellValue(menber.getIdcard());
				row.createCell(2).setCellValue("1".equals(menber.getSex())?"男":"女");
				row.createCell(3).setCellValue(menber.getBir());
				row.createCell(4).setCellValue(menber.getMobile());
				row.createCell(5).setCellValue(menber.getAddress());
			
			}
			String path = "D:/lph/file/"+fileName+".xls";
			OutputStream os = new FileOutputStream(path);
			
			//生成excel
			work.write(os);
			
			os.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
